package games.cube
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Vector3D;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	public class CubeTest extends Sprite
	{
		private var arrMc:Array = [];
		private var con:Sprite = new Sprite();
		
		public function CubeTest()
		{
			
			
			addChild(con);
			
			con.x = 275;
			
			con.y = 200;
			
			var str:String = "123456";
			
			var format:TextFormat=new TextFormat();
			
			format.size = 60;
			
			format.color = 0xff0066;
			
			for (var i:int=0; i<6; i++)
			{
				
				var rect:Sprite = drawRect(200,200,Math.random() * 0xff00ff);
				
				con.addChild(rect);
				
				var txt:TextField=new TextField();
				
				rect.addChild(txt);
				
				txt.x =  -  txt.width / 2;
				
				txt.y =  -  txt.height / 2;
				
				txt.autoSize = TextFieldAutoSize.CENTER;
				
				txt.selectable = false;
				
				txt.text = str.substr(i,1);
				
				txt.setTextFormat(format);
//				
				rect.z = -200 / 2;
				
				arrMc[i] = rect;
				
				i<4?rect.transform.matrix3D.appendRotation(90*i,Vector3D.Y_AXIS):0;
			}
			
			arrMc[4].transform.matrix3D.appendRotation(-90,Vector3D.X_AXIS);
			
			arrMc[5].transform.matrix3D.appendRotation(90,Vector3D.X_AXIS);
			
			addEventListener(Event.ENTER_FRAME,frame);
		}
		
		private function drawRect(w:Number,h:Number,color:uint):Sprite
		{
			
			var sp:Sprite=new Sprite();
			
			sp.graphics.beginFill(color,0.3);
			
			sp.graphics.drawRect(-w/2,-h/2,w,h);
			
			sp.graphics.endFill();
			
			return sp;
			
		}
		
		private function frame(e:Event):void
		{
			
			for (var i:int =0; i<6; i++)
			{
				
				arrMc[i].transform.matrix3D.appendRotation((mouseX-275)/50,Vector3D.Y_AXIS);
				
				arrMc[i].transform.matrix3D.appendRotation((200-mouseY)/50,Vector3D.X_AXIS);
				
			}
			
			arrMc.sortOn("z",18);
			
			for (i=0; i<arrMc.length; i++)
			{
				
				con.setChildIndex(arrMc[i],i);
				
			}
			
		}
	}
}